package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.question.SearchQuestionBo;
import com.zhuss.bean.vo.team.question.DetailQuestionVo;
import com.zhuss.bean.vo.team.question.ListQuestionVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.processor.EnumConfig;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.team.db.dao.ext.QuestionDao;
import com.zhuss.team.db.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/26 13:25
 */
@Component("questionRead")
public class QuestionRead {

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private EnumConfig enumConfig;

    /**
     * 用户列表
     * @param bo
     */
    public Object list(SearchQuestionBo bo) {
        PageVo<ListQuestionVo> result = new PageVo<ListQuestionVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(questionDao.list(bo));
        for (ListQuestionVo item : result.getItems()){
            item.setInvestGroupName(enumConfig.investGroupName(item.getInvestGroup(),item.getArea()));
        }
        result.setTotal(questionDao.countList(bo));
        return result;
    }

    /**
     * 详情
     * @param questionId
     * @throws WpsException
     */
    public Object detail(Long questionId) throws WpsException {
        Object result = questionDao.detail(questionId);

        if (result == null) {
            throw new WpsException(ExceptionCode.数据不存在);
        }
        ((DetailQuestionVo)result).setInvestGroupName(enumConfig.investGroupName( ((DetailQuestionVo)result).getInvestGroup(), ((DetailQuestionVo)result).getArea()));
        return result;
    }

    /**
     * 问题
     * @param area
     * @return
     */
    public Object listQuestion(Integer area) {
        List<Question> questionList = questionDao.listAll(area, null);
        List<ListVo> result = new ArrayList<>();
        transferList(questionList, result);
        return result;
    }

    private void transferList(List<Question> questionList, List<ListVo> result) {
        for (Question item : questionList) {
            ListVo vo = new ListVo();
            vo.setKey(item.getQuestionId());
            vo.setLabel(item.getQuestionName());
            result.add(vo);
        }
    }

    /**
     * 通过listId获取问题列表
     * @param questionIdList
     * @return
     */
    public Object listQuestion(List<Long> questionIdList) {
        List<Question> result = questionDao.listQuestion(questionIdList);
        return result;
    }
}
